import plotly.graph_objects as go
from nicegui import ui
import pandas as pd
from random import random


ui.label('登录系统')
x = ui.input(label='x:')
y = ui.input(label='y:')
z = ui.input(label='z:')



fig = go.Figure()
fig.update_layout(margin=dict(l=0, r=0, t=0, b=0))
plot = ui.plotly(fig).classes('w-full h-40')



def add_trace():
    fig.add_trace(go.Scatter(x=[1, 2], y=[x.value, y.value], mode="markers"))
    plot.update()

ui.button('Add trace', on_click=add_trace)


df = pd.DataFrame(columns=['x', 'y', 'z'])
table = ui.table.from_pandas(df).classes('max-h-40')
def add_to_dataframe():
    # 将新行创建为DataFrame而不是字典
    new_row_df = pd.DataFrame({'x': [x.value], 'y': [y.value], 'z': [z.value]})
    global df,table
    # 使用pd.concat连接新行和旧DataFrame
    df = pd.concat([df, new_row_df], ignore_index=True)
    table = table.clear()
    table = ui.table.from_pandas(df).classes('max-h-40')
    return df

ui.button('Add to DataFrame', on_click=add_to_dataframe)
ui.run()